package suanfa;

public class QuickSumLRArr {
    private static int[][] dictArr;

    public static void main(String[] args) {
        //suanfa.RandomTest5.createRandomArr()
        int[] arr = new int[]{7, 4, 3, 6, 2, 7, 8, 1, 2, 1, 0, 10};
        int sumVal = getSumLR(arr, 0, 3);
        System.out.println(sumVal);
    }

    private static int getSumLR(int[] arr, int L, int R) {
        if (L > R) {
            return 0;
        }
        if (null == dictArr) {
            dictArr = createDict(arr);
            //打印字典
            //printDictArr(dictArr);
        }
        return dictArr[L][R];
    }

    private static void printDictArr(int[][] dictArr) {
        for (int[] ints : dictArr) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t,");
            }
            System.out.println();
        }
    }

    private static int[][] createDict(int[] arr) {
        int[][] dict = new int[arr.length][arr.length];
        for (int i = 0; i < arr.length; i++) {
            int sumVal = 0;
            for (int j = 0; j < arr.length; j++) {
                if (j >= i) {
                    sumVal = sumVal + arr[j];
                    dict[i][j] = sumVal;
                }
            }
        }
        return dict;
    }
}
